//
//  VerifyPwdAlertView.m
//  DriverClient
//
//  Created by 吕晶 on 2020/8/4.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "VerifyPwdAlertView.h"

@interface VerifyPwdAlertView ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UITextField *textField;

@end

@implementation VerifyPwdAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
        self.backgroundColor = HexColorA(0x000000, 0.5);
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(30, (ScreenH - 237) * 0.5, ScreenW - 60, 237)];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.cornerRadius = 6;
        [self addSubview:self.bgView];
        
        UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, self.bgView.nim_width - 40, 19)];
        titleLab.text = @"验证原密码";
        titleLab.font = BoldFontOfSize(18);
        titleLab.textColor = HexColor(0x2B2C44);
        [self.bgView addSubview:titleLab];
        
        UILabel *descLab = [[UILabel alloc] initWithFrame:CGRectMake(titleLab.nim_left, titleLab.nim_bottom + 10, titleLab.nim_width, 0)];
        descLab.text = @"如果忘记原密码，请退出登录后通过忘记密码重新设置。";
        descLab.font = [UIFont systemFontOfSize:14];
        descLab.textColor = HexColor(0x9C9DB3);
        descLab.numberOfLines = 2;
        [descLab sizeToFit];
        [self.bgView addSubview:descLab];
        
        self.textField = [[UITextField alloc] initWithFrame:CGRectMake(descLab.nim_left, descLab.nim_bottom + 20, titleLab.nim_width, 50)];
        self.textField.backgroundColor = HexColor(0xF2F2F5);
        self.textField.layer.cornerRadius = 6;
        self.textField.font = [UIFont systemFontOfSize:15];
        self.textField.textColor = HexColor(0x000000);
        self.textField.placeholder = @"密码";
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 50)];
        leftView.backgroundColor = HexColor(0xF2F2F5);
        self.textField.leftView = leftView;
        self.textField.leftViewMode = UITextFieldViewModeAlways;
        [self.bgView addSubview:self.textField];
        
        UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.bgView.nim_width - 15 - 60, self.bgView.nim_height - 10 - 40, 60, 40)];
        [sureBtn setTitle:@"确认" forState:0];
        [sureBtn setTitleColor:Color_ThemeColor forState:0];
        sureBtn.titleLabel.font = BoldFontOfSize(16);
        [sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:sureBtn];
        
        UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(sureBtn.nim_left - 10 - 60, self.bgView.nim_height - 10 - 40, 60, 40)];
        [cancleBtn setTitle:@"取消" forState:0];
        [cancleBtn setTitleColor:HexColor(0xBDBED9) forState:0];
        cancleBtn.titleLabel.font = BoldFontOfSize(16);
        [cancleBtn addTarget:self action:@selector(disMissView) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:cancleBtn];
    }
    return self;
}

- (void)sureClick {
    if (![FactoryMethod isNull:self.textField.text]) {
        [MBProgressHUD leShowHUDWithMessage:@"验证中" activity:YES];
        [HttpRequest PostWithApi:LogisticsSupplierYanZhengPwd params:@{@"password":self.textField.text} progress:^(NSProgress * _Nonnull progress) {

        } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
            if (code) {
                __weak_self;
                [MBProgressHUD leShowSuccessHUDAddedTo:MAIN_WINDOW message:@"验证成功" delayOperation:^{
                    if (weakself.sureBlock) {
                        weakself.sureBlock();
                    }
                    [weakself disMissView];
                }];
            }
        } fail:^(NSError * _Nonnull error) {

        }];
    }
}

- (void)showInView:(UIView *)view {
    if (!view) {
        return;
    }
    [view addSubview:self];
}

- (void)disMissView {
    [self removeFromSuperview];
}

@end
